package com.fenghuajueli.libbasecoreui.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.blankj.utilcode.util.ToastUtils;
import com.fenghuajueli.libbasecoreui.constants.EventBusConstants;
import com.fenghuajueli.libbasecoreui.data.entity.eventbus.EventEntity;
import com.fenghuajueli.libbasecoreui.user.UserInfoUtils;
import com.fenghuajueli.libbasecoreui.viewmodel.data.LoadingData;
import com.fenghuajueli.libbasecoreui.viewmodel.data.OtherCodeData;
import com.fenghuajueli.libbasecoreui.viewmodel.data.ToastData;

import org.greenrobot.eventbus.EventBus;

public class BaseViewModel extends ViewModel {

    /**
     * 显示等待对话框的数据类
     */
    public MutableLiveData<LoadingData> loadingViewData = new MutableLiveData<>();

    /**
     * 处理其他状态的一些情况
     */
    public MutableLiveData<OtherCodeData> otherStatusData = new MutableLiveData<>();

    public MutableLiveData<Integer> finishActivityData = new MutableLiveData<>();


    /**
     * 等待对话框的默认数据配置类
     */
    protected LoadingData loadingData = new LoadingData(LoadingData.LOADING_STOP);

    protected ToastData toastData = new ToastData("");


    /**
     * 显示loading对话框
     */
    protected void showLoading() {
        loadingData.setStatus(LoadingData.LOADING_START);
        loadingViewData.postValue(loadingData);
    }

    protected void showLoading(String tips) {
        loadingData.setStatus(LoadingData.LOADING_START);
        loadingData.setLoadingTips(tips);
        loadingViewData.postValue(loadingData);
    }

    /**
     * 隐藏loading对话框
     */
    protected void hideLoading() {
        loadingData.setStatus(LoadingData.LOADING_STOP);
        loadingViewData.postValue(loadingData);
    }

    protected void showToast(String content) {
        ToastUtils.showShort(content);
    }


    protected void finishActivity() {
        finishActivityData.postValue(0);
    }


    /**
     * 处理token失败
     */
    protected void handlerTokenError() {
        hideLoading();
        UserInfoUtils.getInstance().clearLoginInfo();
        EventBus.getDefault().post(new EventEntity(EventBusConstants.LOGIN_OUT, null));
//        ARouter.getInstance().build(UserModuleRoute.USER_LOGIN).navigation();
        finishActivity();
    }

}
